package cn.jinbyte.data.changelog.wapper;

import cn.jinbyte.data.changelog.ChangeType;
import cn.jinbyte.data.changelog.annotation.ListLogProp;
import lombok.Getter;
import lombok.experimental.SuperBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * 集合内容差异包装器
 */
@Getter
@SuperBuilder
public class ListChangeWrapper extends ChangeWrapper {
    /**
     * 源实体对象类型
     */
    Class<?> eClass;
    /**
     * 差异类型
     */
    ChangeType changeType;
    /**
     * 旧值
     */
    Object keyOldValue;
    /**
     * 新值
     */
    Object keyNewValue;
    /**
     * 属性差异内容
     */
    List<ChangeWrapper> changeWrappers;


    public List<String> toLogString() {
        List<String> desc = new ArrayList<>();
        ListLogProp collectionLogProp = propField.getAnnotation(ListLogProp.class);
        StringBuilder stringBuilder = new StringBuilder();
        switch (changeType) {
            case ADD:
                stringBuilder.append("新增").append("【").append(collectionLogProp.name()).append(collectionLogProp.subConcat()).append(keyNewValue).append("】").append(":");
                for (ChangeWrapper subChangeWrapper : changeWrappers) {
                    if (subChangeWrapper instanceof PropChangeWrapper propChangeWrapper) {
                        stringBuilder.append(propChangeWrapper.toLogString(changeType));
                    }
                }
                desc.add(stringBuilder.toString());
                break;
            case DELETE:
                stringBuilder.append("删除").append("【").append(collectionLogProp.name()).append(collectionLogProp.subConcat()).append(keyOldValue).append("】").append(":");
                for (ChangeWrapper subChangeWrapper : changeWrappers) {
                    if (subChangeWrapper instanceof PropChangeWrapper propChangeWrapper) {
                        stringBuilder.append(propChangeWrapper.toLogString(changeType));
                    }
                }
                desc.add(stringBuilder.toString());
                break;
            case UPDATE:
                stringBuilder.append("变更").append("【").append(collectionLogProp.name()).append(collectionLogProp.subConcat()).append(keyOldValue).append("】").append(":");
                for (ChangeWrapper subChangeWrapper : changeWrappers) {
                    if (subChangeWrapper instanceof PropChangeWrapper propChangeWrapper) {
                        stringBuilder.append(propChangeWrapper.toLogString(changeType));
                    }
                }
                desc.add(stringBuilder.toString());
                break;
        }
        return desc;
    }
}